package com.masterclient.app.modules.shop.adapter;

import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.TimeUtil;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.R;
import com.masterclient.app.modules.shop.beans.StoreRecommendBean;
import com.masterclient.app.widget.PriceIntegralLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @auther hjian
 * @date 2020/4/10.
 */
public class StoreRecommendAdapter extends BaseQuickAdapter<StoreRecommendBean, StoreRecommendAdapter.ViewHolder> {

    private int orientation;

    /**
     * @param orientation 1:RecyclerView.VERTICAL or 0:RecyclerView.HORIZONTAL
     */
    public StoreRecommendAdapter(int orientation) {
        super(R.layout.item_store_hotsale);
        this.orientation = orientation;
    }

    @Override
    protected void convert(ViewHolder helper, StoreRecommendBean item) {
        helper.itemStoreHotsaleRoot.getLayoutParams().width = ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(
                (orientation == 0 && getItemCount() > 1) ? 43 : 32);
        helper.itemStoreHotsalePic.getLayoutParams().height = helper.itemStoreHotsaleRoot.getLayoutParams().width / 2;

        ViewTransformUtil.glideImageView(mContext, item.getImage(), helper.itemStoreHotsalePic, new Transformation[]{new CenterCrop(),
                new RoundedCornersTransformation(ScreenUtils.dp2px(14), 0, RoundedCornersTransformation.CornerType.TOP)}, R.mipmap.defaults_1);

        helper.itemStoreHotsaleName.setLines(orientation == 0 ? 2 : 1);
        helper.itemStoreHotsaleName.setMaxLines(2);

        helper.itemStoreHotsalePriceshow.setData("0", item.getShow_price());

        if (!TextUtils.isEmpty(item.getLine_price())&&Float.parseFloat(item.getLine_price())!=0&&
                Float.parseFloat(item.getLine_price()) != Float.parseFloat(item.getShow_price())) {
            helper.itemStoreHotsalePriceline.setText("¥"+item.getLine_price());
            helper.itemStoreHotsalePriceline.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            helper.itemStoreHotsalePricelinell.setVisibility(View.VISIBLE);
        }else {
            helper.itemStoreHotsalePricelinell.setVisibility(View.GONE);
        }

        Utils.setDin(helper.itemStoreHotsaleCountdown,mContext);

        if (orientation == RecyclerView.HORIZONTAL) {
            long time = TextUtils.isEmpty(item.getCountdown())?0:Long.parseLong(item.getCountdown());
            if (time > 0){
                helper.itemStoreHotsaleCountdownll.setBackgroundResource(R.drawable.shape_radiu_d9f23051);
                helper.itemStoreHotsaleCountdowndesc.setText("距结束还剩");
                helper.itemStoreHotsaleCountdown.setText(TimeUtil.getShowTimeStr(time*1000));
            } else {
                helper.itemStoreHotsaleCountdownll.setBackgroundResource(R.drawable.shape_radiu_d9bdc0c7);
                helper.itemStoreHotsaleCountdowndesc.setText("已结束");
                helper.itemStoreHotsaleCountdown.setText("");
            }

            helper.itemStoreHotsaleCountdownll.setVisibility(View.VISIBLE);
        }else {
            helper.itemStoreHotsaleCountdownll.setVisibility(View.GONE);
        }

        if (TextUtils.isEmpty(item.getCommission()) || 0 == Double.valueOf(item.getCommission())) {
            helper.itemStoreHotsaleEarn.setVisibility(View.GONE);
        } else {
            helper.itemStoreHotsaleEarn.setVisibility(View.VISIBLE);
            helper.itemStoreHotsaleEarn.setText("赚" + item.getCommission());
        }


            helper.setText(R.id.item_store_hotsale_name, item.getTitle());

        if (item.isShow_vip_price()){
            helper.itemStoreHotsalePricelinell.setVisibility(View.GONE);
            helper.itemStoreHotsaleVippricell.setVisibility(View.VISIBLE);
            helper.itemStoreHotsaleVipprice.setText("¥"+item.getVip_price());
        }else {
            helper.itemStoreHotsaleVippricell.setVisibility(View.GONE);
        }

        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(AroutePath.COMMODITY_DETAIL_ACTIVITY)
                        .withString("id", item.getProduct_id())
                        .withString("source_page","店铺").navigation();
            }
        });

    }


    class ViewHolder extends BaseViewHolder {
        @BindView(R.id.item_store_hotsale_pic)
        ImageView itemStoreHotsalePic;
        @BindView(R.id.item_store_hotsale_countdowndesc)
        TextView itemStoreHotsaleCountdowndesc;
        @BindView(R.id.item_store_hotsale_countdown)
        TextView itemStoreHotsaleCountdown;
        @BindView(R.id.item_store_hotsale_countdownll)
        LinearLayout itemStoreHotsaleCountdownll;
        @BindView(R.id.item_store_hotsale_name)
        TextView itemStoreHotsaleName;
        @BindView(R.id.item_store_hotsale_priceshow)
        PriceIntegralLayout itemStoreHotsalePriceshow;
        @BindView(R.id.item_store_hotsale_earn)
        TextView itemStoreHotsaleEarn;
        @BindView(R.id.item_store_hotsale_priceline)
        TextView itemStoreHotsalePriceline;
        @BindView(R.id.item_store_hotsale_pricelinell)
        LinearLayout itemStoreHotsalePricelinell;
        @BindView(R.id.item_store_hotsale_vip_pricell)
        LinearLayout itemStoreHotsaleVippricell;
        @BindView(R.id.item_store_hotsale_vip_price)
        TextView itemStoreHotsaleVipprice;
        @BindView(R.id.item_store_hotsale_shareearn)
        TextView itemStoreHotsaleShareearn;
        @BindView(R.id.item_store_hotsale_root)
        LinearLayout itemStoreHotsaleRoot;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
